package com.xbongbong.paas.help.symbol;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.enums.DateTimeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.PaasDateUtil;

import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/01/07 15:22
 */

public class DateTimeSymbolHandle extends NumSymbolHandle {


    @Override
    public boolean equals(Object target, PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        Long dateTime = source.getData().getLong(fieldAttrEntity.getAttr());
        if (Objects.isNull(dateTime)) {
            return false;
        }
        if (Objects.equals(DateTimeEnum.TIME.getType(), fieldAttrEntity.getDateType())) {
            Long beforeDate = PaasDateUtil.getBeforeDayBreak(dateTime);
            Long targetTime = PaasDateUtil.getBeforeDayBreak(Long.valueOf(target.toString()));
            return equals(targetTime, beforeDate);
        } else {
            dateTime = PaasDateUtil.getDateFirstSecond(dateTime);
            Long targetTime = PaasDateUtil.getDateFirstSecond(Long.valueOf(target.toString()));
            return equals(targetTime, dateTime);
        }
    }

    @Override
    public boolean noEquals(Object target, PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        Long dateTime = source.getData().getLong(fieldAttrEntity.getAttr());
        if (Objects.isNull(dateTime)) {
            return true;
        }
        if (Objects.equals(DateTimeEnum.TIME.getType(), fieldAttrEntity.getDateType())) {
            Long beforeDate = PaasDateUtil.getBeforeDayBreak(dateTime);
            Long targetTime = PaasDateUtil.getBeforeDayBreak(Long.valueOf(target.toString()));
            return noEquals(targetTime, beforeDate);
        } else {
            dateTime = PaasDateUtil.getDateFirstSecond(dateTime);
            Long targetTime = PaasDateUtil.getDateFirstSecond(Long.valueOf(target.toString()));
            return noEquals(targetTime, dateTime);
        }
    }

    @Override
    public boolean greaterEqual(Object target, PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        Long dateTime = source.getData().getLong(fieldAttrEntity.getAttr());
        if (Objects.isNull(dateTime)) {
            return true;
        }
        if (Objects.equals(DateTimeEnum.TIME.getType(), fieldAttrEntity.getDateType())) {
            dateTime = PaasDateUtil.getBeforeDayBreak(dateTime);
        }
        Number b = (Number) target;
        return greaterEqual(dateTime, b);
    }

    @Override
    public boolean lessEqual(Object target, PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        Long dateTime = source.getData().getLong(fieldAttrEntity.getAttr());
        if (Objects.isNull(dateTime)) {
            return false;
        }
        if (Objects.equals(DateTimeEnum.TIME.getType(), fieldAttrEntity.getDateType())) {
            dateTime = PaasDateUtil.getBeforeDayBreak(dateTime);
        }
        Number b = (Number) target;
        return lessEqual(dateTime, b);
    }

    @Override
    public boolean greaterThan(Object target, PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        Long dateTime = source.getData().getLong(fieldAttrEntity.getAttr());
        if (Objects.isNull(dateTime)) {
            return true;
        }
        if (Objects.equals(DateTimeEnum.TIME.getType(), fieldAttrEntity.getDateType())) {
            dateTime = PaasDateUtil.getBeforeDayBreak(dateTime);
        }
        Number b = (Number) target;
        return greaterThan(dateTime, b);
    }

    @Override
    public boolean lessThan(Object target, PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        Long dateTime = source.getData().getLong(fieldAttrEntity.getAttr());
        if (Objects.isNull(dateTime)) {
            return false;
        }
        if (Objects.equals(DateTimeEnum.TIME.getType(), fieldAttrEntity.getDateType())) {
            dateTime = PaasDateUtil.getBeforeDayBreak(dateTime);
        }
        Number b = (Number) target;
        return lessThan(dateTime, b);
    }

    @Override
    public boolean range(Object target, PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        Long dateTime = source.getData().getLong(fieldAttrEntity.getAttr());
        if (Objects.isNull(dateTime)) {
            return false;
        }
        if (Objects.equals(DateTimeEnum.TIME.getType(), fieldAttrEntity.getDateType())) {
            dateTime = PaasDateUtil.getBeforeDayBreak(dateTime);
        }
        JSONArray jsonObject = JSONObject.parseArray(JSON.toJSONString(target));
        Number min = (Number) jsonObject.get(0);
        Number max = (Number) jsonObject.get(1);
        return range(min, max, dateTime);
    }

    @Override
    public boolean dynamic(Object target, PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        Long dateTime = source.getData().getLong(fieldAttrEntity.getAttr());
        if (Objects.isNull(dateTime)) {
            return false;
        }
        JSONArray jsonObject = JSONObject.parseArray(JSON.toJSONString(target));
        Number min = (Number) jsonObject.get(0);
        Number max = (Number) jsonObject.get(1);
        return dynamic(min, max, dateTime);
    }
}
